package com.baibei.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.LinearLayout;

import com.baibei.widget.PaymentEditText;
import com.baibei.widget.R;
import com.rae.swift.Rx;
import com.rae.widget.dialog.impl.SlideDialog;


/**
 * 输入密码框
 * Created by shenminjie on 2017/8/8.
 * mail:shenminjie@100bei.com
 */

public class PayInputPwdDialog extends DialogFragment {

    PaymentEditText mEtPaymentPassword;
    LinearLayout mLayoutContainer;
    private OnPayFinishListener mListener;

    /**
     * 设置监听
     *
     * @param listener listener
     */
    public void setOnPayFinishListener(OnPayFinishListener listener) {
        this.mListener = listener;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_input_pwd, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        mEtPaymentPassword = view.findViewById(R.id.et_payment_password);
        mLayoutContainer = view.findViewById(R.id.layout_container);
        mEtPaymentPassword.setOnTextFinishListener(new PaymentEditText.OnTextFinishListener() {
            @Override
            public void onTextFinish(CharSequence text, int length) {
                if (mListener != null) {
                    mListener.OnPayFinishListener(text, PayInputPwdDialog.this);
                }
            }
        });
        mEtPaymentPassword.requestFocus();
        //打开输入框
        InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
        view.findViewById(R.id.tv_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });

        //设置宽度
        ViewGroup.LayoutParams params = mLayoutContainer.getLayoutParams();
        params.width = getScreenWidth();
        mLayoutContainer.setLayoutParams(params);
    }

    public int getScreenWidth() {
        WindowManager windowManager = (WindowManager) getContext().getSystemService("window");
        DisplayMetrics dm = new DisplayMetrics();
        windowManager.getDefaultDisplay().getMetrics(dm);
        return dm.widthPixels;
    }


    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return new SlideDialog(getContext());
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        //关闭输入框
        if (getContext() != null) {
            InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(mEtPaymentPassword.getWindowToken(), 0);
        }
        super.onDismiss(dialog);
        if (mListener != null) {
            mListener.onDismiss();
        }
    }

    /**
     * 回调监听
     */
    public interface OnPayFinishListener {

        /**
         * 监听事件
         *
         * @param text     text
         * @param fragment fragment
         */
        void OnPayFinishListener(CharSequence text, DialogFragment fragment);

        /**
         * 关闭
         */
        void onDismiss();
    }
}
